﻿<div class="listpage-left" style="width:150px;">
    <div class="listpage-left-head ">工单类型</div>
    <div class="listpage-left-content">
        <ul id="clsBXJGWorkOrderCategory" />
    </div>
</div>
<div class="listpage-right">
    <div id="dgToolbarBXJGWorkOrderCategory" class="datagrid-toolbar1">
        <div class="toolbar_right">
        </div>
    </div>
    <table id="dgBXJGWorkOrderCategory"></table>
</div>
<script>

    (function () {
        //若在js函数内部直接使用 razor变量，将失去vs2017提供的js代码折叠效果，因此这里搞个变量
        const m = 'BXJGWorkOrderCategory';

        const canAdd = abp.auth.isGranted(m + 'Create');
        const canUpdate = abp.auth.isGranted(m + 'Update');
        const canDelete = abp.auth.isGranted(m + 'Delete');

        const $toolbar = $('#dgToolbar' + m);
        const $toolbarRight = $toolbar.find('.toolbar_right');
        const $dg = $('#dg' + m);
        const $cmenu = $('<div />');
        let expids = new Array();//记录所有已展开的节点id
        const $cls = $('#cls' + m);


        $cls.datalist({
            url: abp.appPath + 'api/services/bxjgworkorder/BXJGWorkOrderType/GetAll',
            queryParams: { forType: 0 },
            textField: 'displayText',
            border: false,
            onSelect: function (index, node) {
                let loadAllParameter = {};
                if (node.value && node.value!=-1) {
                    loadAllParameter.categoryTypeQueryType = 1;
                    loadAllParameter.workOrderTypes = node.value;
                    loadAllParameter.containsNullWorkOrderType = false;
                }
                else if (node.value === 0) {
                    loadAllParameter.categoryTypeQueryType = 0;
                }
                
                $dg.treegrid('load', loadAllParameter);
                console.log(loadAllParameter);
            },
            loadFilter: function (data) {
                data = $.fn.datalist.defaults.loadFilter(data);
                data.rows.splice(0, 0, { value: -1, displayText: '不限' });
                data.rows.splice(1, 0, { value: 0, displayText: '共享的' });
            
                return data;
            },
            onLoadSuccess: function (data) {
                //$cls.datalist('selectRow', 0);
            }
        });

        $cmenu.menu();
        if (canAdd) {
            var $btnAdd = $('<a />').insertBefore($toolbarRight);
            var $btnAddOptions = {
                iconCls: 'icon-add',
                text: abp.localization.bxjgUtils('新增')
            };
            $btnAdd.linkbutton($.extend($btnAddOptions, {
                plain: true,
                onClick: function () {
                    var rows = $dg.treegrid('getSelections');
                    showForm(0, rows.length > 0 ? rows[0].id : $dg.treegrid('getRoot').id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnAddOptions, {
                onclick: function () {
                    $btnAdd.trigger('click');
                }
            }));
        }

        if (canUpdate) {
            var $btnUpdate = $('<a />').insertBefore($toolbarRight);
            var $btnUpdateOptions = {
                iconCls: 'icon-edit',
                text: abp.localization.bxjgUtils('修改')
            };
            $btnUpdate.linkbutton($.extend($btnUpdateOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return false;

                    if (!rows[0].id) {
                        abp.message.warn(abp.localization.bxjgUtils('此节点不允许修改！'));
                        return;
                    }

                    showForm(rows[0].id);
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnUpdateOptions, {
                onclick: function () {
                    $btnUpdate.trigger('click');
                }
            }));
        }

        if (canDelete) {
            var $btnDelete = $('<a />').insertBefore($toolbarRight);
            var $btnDeleteOptions = {
                iconCls: 'icon-remove',
                text: abp.localization.bxjgUtils('删除')
            };
            $btnDelete.linkbutton($.extend($btnDeleteOptions, {
                plain: true,
                onClick: function () {
                    var rows = getSelected($(this).text());
                    if (!rows)
                        return;
                    abp.message.confirm(
                        abp.localization.bxjgUtils('DeleteConfirm'),
                        abp.localization.bxjgUtils('DeleteConfirmPropt'),
                        function (isConfirmed) {
                            if (isConfirmed) {
                                //这里有个诡异的问题，若用两个parent会导致刷新异常
                                abp.ui.setBusy($dg.parent().parent().parent(), abp.services.bxjgworkorder.workOrderCategory.delete({ id: rows[0].id }).done(function (d) {
                                    abp.notify.success(abp.localization.bxjgUtils('删除') + abp.localization.bxjgUtils('成功') + '!');
                                    $dg.treegrid('unselectAll');
                                    $btnRefresh.trigger('click');
                                }));
                            }
                        }
                    );
                }
            }));
            $cmenu.menu('appendItem', $.extend($btnDeleteOptions, {
                onclick: function () {
                    $btnDelete.trigger('click');
                }
            }));
        }

        var $btnRefresh = $('<a />').insertBefore($toolbarRight);
        $btnRefresh.linkbutton({
            iconCls: 'icon-reload',
            text: abp.localization.bxjgUtils('刷新'),
            plain: true,
            onClick: function () {
                $dg.treegrid('reload');
            }
        });
        //str:删除 审核，注意本地化
        function getSelected(str) {
            var rows = $dg.treegrid('getSelections');
            if (rows.length == 0) {//若没有选择，返回的是一个空数组，而非undifined/null
                abp.message.warn(abp.localization.bxjgUtils('请选择要{0}的行', $.trim(str)));
                return false;
            }
            return rows;
        }
        //只有新增和修改会使用编辑表单页面，批量审核或其它需要表单的操作应该另外定义方法而不是高度抽象一个表单窗口
        //em：编辑时的id 若不传则新增
        //parent 父级节点id
        function showForm(id, parent) {
            var $dlg = $('<div id="dlg' + m + '" />');
            if (id)
                $dlg.data('id', id);
            if (parent)
                $dlg.data('parent', parent);

            $dlg.data('onSaved', function () {
                $dlg.dialog('close');
                $dg.treegrid('reload');
            });

            $dlg.dialog({
                iconCls: id ? $btnUpdateOptions.iconCls : $btnAddOptions.iconCls,
                title: (id ? $btnUpdateOptions.text : $btnAddOptions.text) + abp.localization.bxjgWorkOrder(m),
                width: 600,
                height: 400,
                border: false,
                bodyCls: 'dlg-box',
                modal: true,
                href: 'bxjgworkorder/WorkOrderCategory/form.html?_t=' + abp.pageLoadTime.getTime(),
                buttons: [{
                    iconCls: 'icon-ok',
                    text: abp.localization.bxjgUtils('保存'),
                    handler: function () {
                        var callback = $dlg.data('clickSave');
                        if (callback)
                            callback();
                    }
                }, {
                    iconCls: 'icon-undo',
                    text: abp.localization.bxjgUtils('返回'),
                    handler: function () {
                        $dlg.dialog('close');
                    }
                }],
                onClose: function () {
                    $dlg.dialog('destroy');
                }
                //可以通过onload事件初始化控件，如：下拉框
            });
        }

        $dg.treegrid({
            fit: true,
            border: false,
            idField: 'id',
            //dnd:true,这是tree的属性，这里没啥鸟用，treegrid需要扩展js
            treeField: 'text',
            rownumbers: true,
            //queryParams: {
            //    loadParent: true,
            //    categoryTypeQueryType : 1,
            //    workOrderTypes : '___',//首次防止加载数据
            //    containsNullWorkOrderType : false
            //},
            toolbar: '#dgToolbar' + m,
            url: abp.appPath + 'api/services/bxjgworkorder/WorkOrderCategory/GetAll',
            columns: [[
                { field: 'text', title: '名称', width: 300 },
                { field: 'code', title: '编号', width: 200 },
                { field: 'workOrderTypeDisplayName', title: '哪些工单类型可以使用此类别', width: 500 },
                //{
                //    field: 'workOrderTypeDisplayName', title: '关联的工单类型', width: 500, align: 'center', formatter: function (value, row, index) {
                //        if (!row.workOrderTypes)
                //            return '';
                //        let str = '';
                //        row.workOrderTypes.forEach(function (curr, idx) {
                //            str += curr.workOrderTypeDisplayName;
                //            if (curr.isDefault)
                //                str += '(默认)';
                //            str += '，';
                //        });
                //        return str;
                //    }
                //},
                { field: 'isDefault', title: '默认', width: 50, align: 'center', formatter: abp.easyui.datagridBool }
            ]],
            onLoadSuccess: function (node, data) {
                $(this).treegrid('enableDnd');
                expids.forEach(function (value, index) {
                    $dg.treegrid('expand', value);
                });
            },
            onDrop: function (target, source, point) {
                switch (point) {
                    case 'top':
                        point = 0;
                        break;
                    case 'bottom':
                        point = 1;
                        break;
                    case 'append':
                        point = 2;
                        break;
                }
                abp.ui.setBusy($dg.parent().parent().parent(), abp.services.bxjgworkorder.workOrderCategory.move({ targetId: target.id, id: source.id, moveType: point }).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('修改') + abp.localization.bxjgUtils('成功') + '!');
                    $btnRefresh.trigger('click');
                }));
            },
            onContextMenu: function (e, row) {
                if (!row)
                    return;
                e.preventDefault();//禁止浏览器默认右键菜单
                //可以在这里临时创建右键菜单，则可以通过闭包访问row
                $dg.treegrid('select', row.id);
                $cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onDblClickCell: function (field, row) {
                $dg.treegrid('toggle', row.id);
            },
            onExpand: function (row) {
                if ($.inArray(row.id, expids) == -1)
                    expids.push(row.id);
            },
            onCollapse: function (row) {
                expids.splice($.inArray(row.id, expids), 1);
            }
        });
    }());
</script>


